var labelData = [],
    addressData = '地点：',
    tId = '',
    modularImg = [],
    titleForm = new FormData(),
    token = localStorage.getItem("token"),
    modularNum = 0

//添加封面
$(".addCover input").on("change", function (e) {
    if ($(this).val()) {
        $(".addCover").css("display", "block").addClass("addCoverWidth")
        $(".addCoverIcon").css("display", "none")
    }
    $(".cover").remove()
    $(".addCover input").after(`<section class="cover"><img src=""></section>`)
    let file = this.files[0];
    let reader = new FileReader();
    reader.addEventListener("load", function () {
        $(".addCover img").attr("src", reader.result)
        //封面传入表单
        titleForm.append('titleImg', file)
        $(".cover").on("click", function () {
            $(this).find("img").after(`<span></span>`)
            setTimeout(function () {
                $(this).find("span").remove()
            }, 3000)
            $(this).find("span").on("click", function () {
                $(this).parent().remove()
                $(".addCover").removeClass("addCoverWidth")
                $(".addCoverIcon").css("display", "block")
                titleForm.delete('titleImg')
            })
        })
    }, false);
    reader.readAsDataURL(file);
})

$(".addText").html(`<section class="modular-last modular-text">
            <section class="text_box focus" contenteditable="true"></section>
        </section>`)
$(".addTitle section").on("input", function () {
    $(this).removeClass("title-focus")
})

//输入正文内容
$(".text_box").on("input", function () {
    $(this).removeClass("focus")
})

//右下角添加图片按钮
$(".addImages input").on("change", function () {
    let file = this.files[0]
    let reader = new FileReader()
    reader.addEventListener("load", function (e) {
        let thatText = $(".modular-last")
        thatText.find("section").removeClass("focus")
        if ($(thatText).find("section").text().length == 0) {
            modularImg[modularNum] = file
            $(".modular-last").before(`<section class="modular-img">
                        <section class="modular-img-box" data-modularNum="${modularNum++}"><img src="${e.target.result}" class="modular-img"></section>
                </section>`)
        } else {
            modularImg[modularNum] = file
            $(".modular-last").after(`<section class="modular-img">
                        <section class="modular-img-box" data-modularNum="${modularNum++}"><img src="${e.target.result}" class="modular-img"></section>
                </section>`).next().after(`<section class="modular-last modular-text">
                    <section class="text_box" contenteditable="true"></section>
                </section>`)
            $(thatText).removeClass("modular-last")
        }
        $(".modular-last").focus()
        $(".modular-img-box").on("click", function () {
            let thatImg = this
            $(this).find("img").after(`<span></span>`)
            setTimeout(function () {
                $(thatImg).find("span").remove()
            }, 3000)
            $(this).find("span").on("click", function () {
                $(this).parents(".modular-img").remove()
                modularImg[parseInt($(thatImg).attr("data-modularnum"))] = ''
            })
        })
    })
    reader.readAsDataURL(file)
})

//添加标签按钮
$(".addInformation .addLabel").on("click", function () {
    $(".label").remove()
    if (labelData != '') {
        labelData = labelData.map((value) => {
            return value.substr(1)
        }).toString().replace(/,/g, ' ')
    }
    $(this).before(`<input type="text" value="` + labelData + `" class="labelInput">`)
    $(".addInformation input").focus().on("blur", function () {
        labelData = $(this).val().trim().split(' ').map((value) => {
            return '#' + value
        })
        $.each(labelData, function (i, value) {
            $(".addLabel").before(`<span class="label">` + value + `</span>`)
        })
        $(this).remove()
    })
})

//添加地址
$(".addInformation .addAddress").on("click", function () {
    $(".address").remove()
    $(this).before(`<input type="text" class="addressInput" value="${addressData.split("地点：")[1]}">`)
    $(".addInformation input").focus().on("blur", function () {
        addressData = "地点：" + $(this).val()
        $(this).remove()
        $(".addAddress").before(`<span class="address">` + addressData + `</span>`)
    })
})

//发布
$(".release").on("click", function () {
    if ($("#title").text().length == 0) {
        layer.msg('请输入内容');
    } else {
        //标题传入表单
        titleForm.append('title', $("#title").text())
        let label = ''
        $.each($(".label"), function (i, val) {
            label += ' ' + $(".label").eq(i).text()
        })
        titleForm.append('label', label)
        titleForm.append('position', $(".address").text().substr(3))
        if (titleForm.get("titleImg")) {
            titleForm.append("haveImg", 1)
        } else {
            titleForm.append("haveImg", 0)
        }
        //获取tId
        tId = SendFileajax("http://119.23.46.241:8080/admin/release/releasetitle", "post", titleForm, token).data

        let textNum = 0,
            imgNum = 0
        $.each($(".addText").children(), function (i, val) {
            if (val.className.includes("modular-text")) {
                let text = $(".text_box").eq(textNum)
                textNum++
                contentForm = new FormData()
                contentForm.append("tId", String(tId))
                contentForm.append("content", text.text())
                contentForm.append("img", '')
                contentForm.append("haveImg", parseInt(0))
                contentForm.append("haveContent", parseInt(1))
                SendFileajax("http://119.23.46.241:8080/admin/release/releasecontent", "post", contentForm, token)
            } else {
                let contentForm = new FormData()
                contentForm.append("tId", String(tId))
                contentForm.append("content", '')
                contentForm.append("img", modularImg[imgNum++])
                contentForm.append("haveImg", parseInt(1))
                contentForm.append("haveContent", parseInt(0))
                SendFileajax("http://119.23.46.241:8080/admin/release/releasecontent", "post", contentForm, token)
            }
            setTimeout(function () {
                stop()
                layer.confirm('上传成功', {
                    btn: ['确定']
                    , btn1: function (index, layero) {
                        window.location.href = "administratorPersonalPage.html"
                        move()
                    }
                });
            }, 4000)
        })
    }
})
